package com.eco.fanliapp.dataoke;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eco.fanliapp.utils.DtkHttpUtils;
import com.eco.fanliapp.utils.JsonUtils;
import com.eco.fanliapp.utils.SignMD5Util;
import com.eco.fanliapp.vo.DaoTaoKeSuperCategoryInfo;
import com.eco.fanliapp.vo.respone.DTKResponse;
import com.eco.fanliapp.vo.respone.DTKTbServiceResponse;
import com.eco.fanliapp.vo.respone.DaTaoKeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.net.URLEncoder;
import java.util.*;

@Service
@Slf4j
public class DaTaoKeItemInfoService {

    public static void main(String[] args) {
        try {
            TreeMap<String, String> paraMap = new TreeMap<>();
            paraMap.put("appKey", DaTaoKeConfig.DaTaoKe_CATEGORY_APPKEY);
            paraMap.put("version", "v1.2.1");
            paraMap.put("type", "2");
            paraMap.put("pageId", "1");
            paraMap.put("pageSize", "20");
            paraMap.put("keyWords", "数据线苹果");
            paraMap.put("sort", "price_asc");
            paraMap.put("sign", SignMD5Util.getSignStr(paraMap, DaTaoKeConfig.DaTaoKe_CATEGORY_APPSECRET));

            String result = null;

//            result = DtkHttpUtils.sendGet(DaTaoKeConfig.DaTaoKe_LISTSUPERGOODS_URL, paraMap);
            result = DtkHttpUtils.sendGet("https://openapi.dataoke.com/api/goods/list-super-goods", paraMap);


            log.info(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据商品id查询详情
     *
     * @param itemId
     * @return
     */
    public DTKResponse itemInfoByItemId(String itemId) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.1.1");
        paraMap.put("appKey", DaTaoKeConfig.DaTaoKe_APPKEY);
        paraMap.put("goodsId", itemId);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, DaTaoKeConfig.DaTaoKe_APPSECRET));
        DTKResponse dtkResponse = null;
        try {
            String result = DtkHttpUtils.sendGet(DaTaoKeConfig.DaTaoKe_URL, paraMap);
            dtkResponse = JSON.parseObject(result, DTKResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtkResponse;
    }

    /**
     * 超级分类
     *
     * @return
     */
    public DaoTaoKeSuperCategoryInfo getSuperCategory() {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.1.0");
        paraMap.put("appKey", DaTaoKeConfig.DaTaoKe_CATEGORY_APPKEY);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, DaTaoKeConfig.DaTaoKe_CATEGORY_APPSECRET));
        DaoTaoKeSuperCategoryInfo info = null;
        try {
            String result = DtkHttpUtils.sendGet(DaTaoKeConfig.DaTaoKe_CATEGORY_URL, paraMap);
            info = JSON.parseObject(result, DaoTaoKeSuperCategoryInfo.class);
            log.info(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 商品列表
     *
     * @return
     */
    public String getGoodsList(String pageSize, String pageId) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.1.1");
        paraMap.put("appKey", DaTaoKeConfig.DaTaoKe_CATEGORY_APPKEY);
        paraMap.put("pageSize", pageSize);
        paraMap.put("pageId", pageId);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, DaTaoKeConfig.DaTaoKe_CATEGORY_APPSECRET));
        List<DTKResponse.DataBean> info = null;
        String result = null;
        try {
            result = DtkHttpUtils.sendGet(DaTaoKeConfig.DaTaoKe_GOODSLIST_URL, paraMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 大淘客白菜专区（免单）商品列表
     *
     * @return
     */
    public String getGoodsFreeList(String pageSize, String pageId) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.1.1");
        paraMap.put("appKey", DaTaoKeConfig.DaTaoKe_CATEGORY_APPKEY);
        paraMap.put("pageSize", pageSize);
        paraMap.put("pageId", pageId);
        paraMap.put("priceUpperLimit", "7");
        paraMap.put("commissionRateLowerLimit", "70");
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, DaTaoKeConfig.DaTaoKe_CATEGORY_APPSECRET));
        List<DTKResponse.DataBean> info = null;
        String result = null;
        try {
            result = DtkHttpUtils.sendGet(DaTaoKeConfig.DaTaoKe_GOODSLIST_URL, paraMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 失效商品
     *
     * @return
     */
    public String getStaleGoods(String pageSize, String pageId) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.0.1");
        paraMap.put("appKey", DaTaoKeConfig.DaTaoKe_CATEGORY_APPKEY);
        paraMap.put("pageSize", pageSize);
        paraMap.put("pageId", pageId);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, DaTaoKeConfig.DaTaoKe_CATEGORY_APPSECRET));
        List<DTKResponse.DataBean> info = null;
        String result = null;
        try {
            result = DtkHttpUtils.sendGet(DaTaoKeConfig.DaTaoKe_STALEGOODS_URL, paraMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 商品更新
     *
     * @return
     */
    public String getNewestGoods(String pageSize, String pageId) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("version", "v1.1.0");
        paraMap.put("appKey", DaTaoKeConfig.DaTaoKe_CATEGORY_APPKEY);
        paraMap.put("pageSize", pageSize);
        paraMap.put("pageId", pageId);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, DaTaoKeConfig.DaTaoKe_CATEGORY_APPSECRET));
        List<DTKResponse.DataBean> info = null;
        String result = null;
        try {
            result = DtkHttpUtils.sendGet(DaTaoKeConfig.DaTaoKe_NEWESTGOODS_URL, paraMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 大淘客超级搜索
     *
     * @return
     */
    public String getDaTaoKeSearchGoods(String keyword, String sort, Long pageSize, String pageNum) {
        TreeMap<String, String> paraMap = new TreeMap<>();
        paraMap.put("appKey", DaTaoKeConfig.DaTaoKe_CATEGORY_APPKEY);
        paraMap.put("version", "v1.2.1");
        paraMap.put("type", "2");
        paraMap.put("pageId", pageNum);
        paraMap.put("pageSize", pageSize.toString());
        paraMap.put("keyWords", keyword);
        paraMap.put("sort", sort);
        paraMap.put("sign", SignMD5Util.getSignStr(paraMap, DaTaoKeConfig.DaTaoKe_CATEGORY_APPSECRET));

        String result = null;
        try {
            result = DtkHttpUtils.sendGet(DaTaoKeConfig.DaTaoKe_LISTSUPERGOODS_URL, paraMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
